<template>
  <el-card>
    <el-input v-model="name" inline style="width:200px" />
    <el-button
      type="primary"
      icon="el-icon-search"
      style="margin-left:10px"
      @click="inquire"
    >
      查询
    </el-button>
    <el-button style="margin-left:10px" @click="name = ''">
      清空
    </el-button>
    <el-row>
      <!-- 添加删除 -->
      <el-button
        style="margin:20px 10px"
        type="primary"
        @click="centerDialogVisible = true"
      >
        添加
      </el-button>
      <el-button style="margin:20px 10px" type="danger" @click="del">
        批量删除
      </el-button>
      <!-- 弹窗 -->
      <el-dialog
        title="添加角色"
        :visible.sync="centerDialogVisible"
        width="30%"
      >
        <span style="font-size:14px;color:#666">请输入新名称</span>
        <el-input v-model="addRoleName" style="margin:20px 0" />
        <span slot="footer">
          <el-button @click="centerDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRole">
            确 定
          </el-button>
        </span>
      </el-dialog>
    </el-row>
    <!-- 表格 -->
    <el-table
      :data="roleList"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column width="50" type="selection" />
      <el-table-column label="index" type="index" width="70" align="center" />
      <el-table-column prop="roleName" label="角色名称" width="800">
        <template v-slot="{ row, $index }">
          <span v-show="!row.deleted">{{ row.roleName }}</span>
          <input
            v-show="row.deleted"
            :ref="$index"
            style="width:80%;height:26px;margin-right:30px;outline:none;font-size:12px"
          />
          <el-button
            v-show="row.deleted"
            type="warning"
            icon="el-icon-refresh"
            size="mini"
            @click="cancel(row, $index)"
          >
            取消
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row, $index }">
          <!-- 分配权限 -->
          <el-button
            icon="el-icon-info"
            size="mini"
            type="info"
            @click="setPurview(row)"
          />
          <el-button
            v-show="row.deleted"
            icon="el-icon-check"
            size="mini"
            type="primary"
            @click="verifyRevise(row, $index)"
          />
          <el-button
            v-show="!row.deleted"
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="modification(row, $index)"
          />
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="danger"
            @click="delSave(row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
</template>

<script>
import {
  getRoleList,
  addSave,
  delRole,
  delAll,
  roleUpdata
} from '@/api/acl/role'

export default {
  name: 'Role',
  data() {
    return {
      name: '',
      roleList: [],
      page: 1,
      limit: 5,
      total: 0,
      centerDialogVisible: false,
      addRoleName: '',
      selectAll: []
    }
  },
  async mounted() {
    this.getRoleValueList()
  },
  methods: {
    // 请求数据
    async getRoleValueList() {
      const { page, limit } = this
      const res = await getRoleList({ page, limit })
      this.roleList = res.items
      this.total = res.total
    },
    handleSizeChange(val) {
      this.limit = val
      this.getRoleValueList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getRoleValueList()
    },
    // 查询数据
    async inquire() {
      const { page, limit, name } = this
      const res = await getRoleList({ page, limit, name })
      this.roleList = res.items
      this.total = res.total
    },
    // 添加
    async addRole() {
      const data = { roleName: this.addRoleName }
      await addSave(data)
      this.$message({
        message: '添加成功',
        type: 'success'
      })
      this.getRoleValueList()
      this.centerDialogVisible = false
    },
    // 删除单个
    async delSave(row) {
      // console.log(row.id)
      await delRole(row.id)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getRoleValueList()
    },
    // 选中触发
    async handleSelectionChange(val) {
      // this.multipleSelection = val
      console.log(val)
      this.selectAll = val.map(item => {
        return item.id
      })
    },
    // 删除多个
    async del() {
      await delAll(this.selectAll)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getRoleValueList()
    },
    // 修改
    modification(row, index) {
      row.deleted = true
      this.$nextTick(() => {
        this.$refs[index].value = row.roleName
      })
    },
    // 取消修改
    cancel(row) {
      row.deleted = false
    },
    // 确认修改
    async verifyRevise(row, index) {
      if (!this.$refs[index].value) {
        this.$message({
          message: '请输入修改内容',
          type: 'warning'
        })
        return
      }
      await roleUpdata(this.$refs[index].value, row.id)
      this.$message({
        message: '修改成功',
        type: 'success'
      })
      row.deleted = false
      this.getRoleValueList()
    },
    //分配权限
    setPurview(row) {
      this.$router.history.push({
        name: 'Auth',
        query: {
          roleName: row.roleName
        },
        params: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style scoped></style>
